package com.herui.ota.service.iml;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.herui.ota.dao.mysql.DeviceOtaSoftMapper;
import com.herui.ota.entity.DeviceOta;
import com.herui.ota.entity.DeviceOtaSoft;
import com.herui.ota.enums.OpsTypeEnum;
import com.herui.ota.enums.ResponseEnum;
import com.herui.ota.exception.ServiceException;
import com.herui.ota.pojo.form.DeviceOtaConfigFilterForm;
import com.herui.ota.pojo.form.DeviceOtaSoftFilterForm;
import com.herui.ota.pojo.form.DeviceOtaSoftForm;
import com.herui.ota.pojo.vo.DeviceOtaSoftVo;
import com.herui.ota.service.DeviceOtaService;
import com.herui.ota.service.DeviceOtaSoftService;
import com.herui.ota.service.OpsRecordService;
import com.mlnx.common.form.PageForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * author name: amanda.shan
 * create time: 2024-05-07 14:55:00
 */
@Service
public class DeviceOtaSoftServiceIml extends BaseService<DeviceOtaSoftMapper, DeviceOtaSoft> implements DeviceOtaSoftService {

    @Autowired
    private DeviceOtaService deviceOtaService;

    @Autowired
    private OpsRecordService opsRecordService;

    @Autowired
    @Override
    public void setBaseMapper(DeviceOtaSoftMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Override
    public IPage page(PageForm pageForm, Object filter) throws IllegalAccessException {

        Page page = baseMapper.listDeviceOtaSoftVoPage(new Page(pageForm.getCurrent(), pageForm.getSize()),
                (DeviceOtaSoftFilterForm) filter);
        return page;
    }

    @Override
    public void save(DeviceOtaSoftForm deviceOtaSoftForm) {

        List<String> strings = getRights();
        if (strings == null || !strings.contains("gujian_edit")){
            throw new ServiceException(ResponseEnum.ROLE_NO);
        }

        DeviceOta deviceOta = deviceOtaService.getDeviceOta(deviceOtaSoftForm.getBrand(),
                deviceOtaSoftForm.getMarket(), deviceOtaSoftForm.getPrintType(), deviceOtaSoftForm.getSeries());
        if (deviceOta == null) {
            throw new ServiceException(ResponseEnum.OTA_NOT_EXIST);
        }
        DeviceOtaSoft deviceOtaSoft = new DeviceOtaSoft();
        BeanUtil.copyProperties(deviceOtaSoftForm, deviceOtaSoft);
        deviceOtaSoft.setOtaId(deviceOta.getId());
        deviceOtaSoft.setNumb(System.currentTimeMillis()+"");
        save(deviceOtaSoft);

        opsRecordService.save(OpsTypeEnum.CREATE_OTA_SOFT, deviceOtaSoft.getId(), JSON.toJSONString(deviceOtaSoft));
    }

    @Transactional
    @Override
    public void update(Integer id, DeviceOtaSoftForm deviceOtaSoftForm) {

        DeviceOtaSoft otaSoft = getById(id);
        if ( "ONLINE".equals(deviceOtaSoftForm.getState())){
            int count = count(new QueryWrapper<DeviceOtaSoft>().eq("version", otaSoft.getVersion()).eq("state",
                    "ONLINE"));
            if (count > 0){
                throw new ServiceException(ResponseEnum.OTA_SOFT_IS_ONLINE);
            }
        }

        DeviceOta deviceOta = deviceOtaService.getDeviceOta(deviceOtaSoftForm.getBrand(),
                deviceOtaSoftForm.getMarket(), deviceOtaSoftForm.getPrintType(), deviceOtaSoftForm.getSeries());
        if (deviceOta == null) {
            throw new ServiceException(ResponseEnum.OTA_NOT_EXIST);
        }
        DeviceOtaSoft deviceOtaSoft = new DeviceOtaSoft();
        BeanUtil.copyProperties(deviceOtaSoftForm, deviceOtaSoft);
        deviceOtaSoft.setOtaId(deviceOta.getId());
        deviceOtaSoft.setId(id);
        updateById(deviceOtaSoft);
    }

    @Override
    public void update(Integer id, String state) {

        checkRightIn("gujian_state_config");

        DeviceOtaSoft deviceOtaSoft = new DeviceOtaSoft();
        deviceOtaSoft.setState(state);
        deviceOtaSoft.setId(id);
        updateById(deviceOtaSoft);
    }

    @Transactional
    @Override
    public boolean updateById(DeviceOtaSoft entity) {

        if (!StringUtils.isEmpty(entity.getState())){
            checkRightIn("gujian_edit", "gujian_state_config");
        }

        if (!StringUtils.isEmpty(entity.getPath()) || !StringUtils.isEmpty(entity.getVersion()) ||
                entity.getOtaId() != null){
            checkRight("gujian_edit");
        }
        opsRecordService.save(OpsTypeEnum.UPDATE_OTA_SOFT, entity.getId(), JSON.toJSONString(entity));
        return super.updateById(entity);
    }

    @Override
    public List<Object> list(Object filter) throws IllegalAccessException {
        QueryWrapper wrapper = this.getQueryWrapper(filter);
        wrapper.orderByDesc("create_time");
        return super.list(wrapper);
    }

    @Override
    public DeviceOtaSoftVo getDeviceOtaSoft(String series, String channel) {
        return baseMapper.findDeviceOtaSoft(series, channel);
    }

    @Override
    public boolean removeById(Serializable id) {
        checkRight("gujian_edit");
        DeviceOtaSoft deviceOta = getById(id);
        opsRecordService.save(OpsTypeEnum.DELETE_OTA_SOFT, deviceOta.getId(), JSON.toJSONString(deviceOta));
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {

        checkRight("gujian_edit");
        idList.stream().forEach(id ->{
            DeviceOtaSoft deviceOta = getById(id);
            opsRecordService.save(OpsTypeEnum.DELETE_OTA_SOFT, deviceOta.getId(), JSON.toJSONString(deviceOta));
        });

        return super.removeByIds(idList);
    }
}

